{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# who_killed_agatha"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/who_killed_agatha.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/who_killed_agatha.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Who killed agatha? (The Dreadsbury Mansion Murder Mystery) in Google CP\n",
    "  Solver.\n",
    "\n",
    "  This is a standard benchmark for theorem proving.\n",
    "\n",
    "  http://www.lsv.ens-cachan.fr/~goubault/H1.dist/H1.1/Doc/h1003.html\n",
    "  '''\n",
    "  Someone in Dreadsbury Mansion killed Aunt Agatha.\n",
    "  Agatha, the butler, and Charles live in Dreadsbury Mansion, and\n",
    "  are the only ones to live there. A killer always hates, and is no\n",
    "  richer than his victim. Charles hates noone that Agatha hates. Agatha\n",
    "  hates everybody except the butler. The butler hates everyone not richer\n",
    "  than Aunt Agatha. The butler hates everyone whom Agatha hates.\n",
    "  Noone hates everyone. Who killed Agatha?\n",
    "  '''\n",
    "\n",
    "  Originally from F. J. Pelletier:\n",
    "  Seventy-five problems for testing automatic theorem provers.\n",
    "  Journal of Automated Reasoning, 2: 216, 1986.\n",
    "\n",
    "  Note1: Since Google CP Solver/Pythons (currently) don't have\n",
    "         special support for logical operations on decision\n",
    "         variables (i.e. ->, <->, and, or, etc), this model\n",
    "         use some IP modeling tricks.\n",
    "\n",
    "  Note2: There are 8 different solutions, all stating that Agatha\n",
    "         killed herself\n",
    "\n",
    "  Compare with the following models:\n",
    "  * Choco   : http://www.hakank.org/choco/WhoKilledAgatha.java\n",
    "  * Choco   : http://www.hakank.org/choco/WhoKilledAgatha_element.java\n",
    "  * Comet   : http://www.hakank.org/comet/who_killed_agatha.co\n",
    "  * ECLiPSE : http://www.hakank.org/eclipse/who_killed_agatha.ecl\n",
    "  * Gecode  : http://www.hakank.org/gecode/who_killed_agatha.cpp\n",
    "  * JaCoP   : http://www.hakank.org/JaCoP/WhoKilledAgatha.java\n",
    "  * JaCoP   : http://www.hakank.org/JaCoP/WhoKilledAgatha_element.java\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/who_killed_agatha.mzn\n",
    "  * Tailor/Essence': http://www.hakank.org/tailor/who_killed_agatha.eprime\n",
    "  * SICStus : http://hakank.org/sicstus/who_killed_agatha.pl\n",
    "  * Zinc    :http://hakank.org/minizinc/who_killed_agatha.zinc\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def var_matrix_array(solver, rows, cols, lb, ub, name):\n",
    "  x = []\n",
    "  for i in range(rows):\n",
    "    t = []\n",
    "    for j in range(cols):\n",
    "      t.append(solver.IntVar(lb, ub, \"%s[%i,%i]\" % (name, i, j)))\n",
    "    x.append(t)\n",
    "  return x\n",
    "\n",
    "\n",
    "def flatten_matrix(solver, m, rows, cols):\n",
    "  return [m[i][j] for i in range(rows) for j in range(cols)]\n",
    "\n",
    "\n",
    "def print_flat_matrix(m_flat, rows, cols):\n",
    "  for i in range(rows):\n",
    "    for j in range(cols):\n",
    "      print(m_flat[i * cols + j].Value(), end=\" \")\n",
    "    print()\n",
    "  print()\n",
    "\n",
    "\n",
    "def main(the_killers):\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver(\"Who killed agatha?\")\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 3\n",
    "  agatha = 0\n",
    "  butler = 1\n",
    "  charles = 2\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "  the_killer = solver.IntVar(0, 2, \"the_killer\")\n",
    "  the_victim = solver.IntVar(0, 2, \"the_victim\")\n",
    "\n",
    "  hates = var_matrix_array(solver, n, n, 0, 1, \"hates\")\n",
    "  richer = var_matrix_array(solver, n, n, 0, 1, \"richer\")\n",
    "\n",
    "  hates_flat = flatten_matrix(solver, hates, n, n)\n",
    "  richer_flat = flatten_matrix(solver, richer, n, n)\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "\n",
    "  # Agatha, the butler, and Charles live in Dreadsbury Mansion, and\n",
    "  # are the only ones to live there.\n",
    "\n",
    "  # A killer always hates, and is no richer than his victim.\n",
    "  # solver.Add(hates[the_killer, the_victim] == 1)\n",
    "  solver.Add(solver.Element(hates_flat, the_killer * n + the_victim) == 1)\n",
    "\n",
    "  # solver.Add(richer[the_killer, the_victim] == 0)\n",
    "  solver.Add(solver.Element(richer_flat, the_killer * n + the_victim) == 0)\n",
    "\n",
    "  # define the concept of richer: no one is richer than him-/herself\n",
    "  for i in range(n):\n",
    "    solver.Add(richer[i][i] == 0)\n",
    "\n",
    "  # (contd...) if i is richer than j then j is not richer than i\n",
    "  #  (i != j) => (richer[i,j] = 1) <=> (richer[j,i] = 0),\n",
    "  for i in range(n):\n",
    "    for j in range(n):\n",
    "      if i != j:\n",
    "        solver.Add((richer[i][j] == 1) == (richer[j][i] == 0))\n",
    "\n",
    "  # Charles hates noone that Agatha hates.\n",
    "  # forall i : Range .\n",
    "  #  (hates[agatha, i] = 1) => (hates[charles, i] = 0),\n",
    "  for i in range(n):\n",
    "    solver.Add((hates[agatha][i] == 1) <= (hates[charles][i] == 0))\n",
    "\n",
    "  # Agatha hates everybody except the butler.\n",
    "  solver.Add(hates[agatha][charles] == 1)\n",
    "  solver.Add(hates[agatha][agatha] == 1)\n",
    "  solver.Add(hates[agatha][butler] == 0)\n",
    "\n",
    "  # The butler hates everyone not richer than Aunt Agatha.\n",
    "  # forall i : Range .\n",
    "  #  (richer[i, agatha] = 0) => (hates[butler, i] = 1),\n",
    "  for i in range(n):\n",
    "    solver.Add((richer[i][agatha] == 0) <= (hates[butler][i] == 1))\n",
    "\n",
    "  # The butler hates everyone whom Agatha hates.\n",
    "  # forall i : Range .\n",
    "  #  (hates[agatha, i] = 1) => (hates[butler, i] = 1),\n",
    "  for i in range(n):\n",
    "    solver.Add((hates[agatha][i] == 1) <= (hates[butler][i] == 1))\n",
    "\n",
    "  # Noone hates everyone.\n",
    "  # forall i : Range .\n",
    "  #   (sum j : Range . hates[i,j]) <= 2,\n",
    "  for i in range(n):\n",
    "    solver.Add(solver.Sum([hates[i][j] for j in range(n)]) <= 2)\n",
    "\n",
    "  # Who killed Agatha?\n",
    "  solver.Add(the_victim == agatha)\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  solution = solver.Assignment()\n",
    "  solution.Add(the_killer)\n",
    "  solution.Add(the_victim)\n",
    "  solution.Add(hates_flat)\n",
    "  solution.Add(richer_flat)\n",
    "\n",
    "  # db: DecisionBuilder\n",
    "  db = solver.Phase(hates_flat + richer_flat, solver.CHOOSE_FIRST_UNBOUND,\n",
    "                    solver.ASSIGN_MIN_VALUE)\n",
    "\n",
    "  solver.NewSearch(db)\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    print(\"the_killer:\", the_killer.Value())\n",
    "    the_killers[the_killer.Value()] += 1\n",
    "    print(\"the_victim:\", the_victim.Value())\n",
    "    print(\"hates:\")\n",
    "    print_flat_matrix(hates_flat, n, n)\n",
    "    print(\"richer:\")\n",
    "    print_flat_matrix(richer_flat, n, n)\n",
    "    print()\n",
    "    num_solutions += 1\n",
    "\n",
    "  solver.EndSearch()\n",
    "\n",
    "  print()\n",
    "  print(\"num_solutions:\", num_solutions)\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "\n",
    "\n",
    "the_killers = defaultdict(int)\n",
    "p = [\"agatha\", \"butler\", \"charles\"]\n",
    "main(the_killers)\n",
    "\n",
    "print(\"\\n\")\n",
    "for k in the_killers:\n",
    "  print(\"the killer %s was choosen in %i solutions\" % (p[k], the_killers[k]))\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
